<template>
	<section>
		<!-- 新增编辑弹出框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible">
			<el-form :model="form">
				<el-form-item label="机构名称" >
					<el-input v-model="form.companyName" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="营业执照" >
					<el-input v-model="form.companyNum" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="机构类型">
					<el-select v-model="form.typeId" placeholder="请选择机构类型">
						<el-option
								v-for="item in TenantTypes"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="超级管理员">
					<el-select v-model="form.employeeId" placeholder="请选择超级管理员">
						<el-option
								v-for="item in employees"
								:key="item.id"
								:label="item.username"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="公司地址" >
					<el-input v-model="form.address" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="LOGO" >
					<el-input v-model="form.logo" autocomplete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button @click="saveTenant" type="primary">确 定</el-button>
			</div>
		</el-dialog>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="filters" :inline="true">
				<el-form-item>
					<el-input v-model="filters.keywords" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTenants">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="addTenants" >新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tenants" v-loading="listLoading" highlight-current-row  style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<!--索引值,为什么不用id,id不序号-->
			<el-table-column type="index" width="60">
			</el-table-column>
			<!--其他都设置值,只有一个不设置值就自动适应了-->
			<el-table-column prop="companyName" label="机构名称">
			</el-table-column>
			<el-table-column prop="companyNum" label="营业执照">
			</el-table-column>
			<el-table-column prop="type.name" label="机构类型">
			</el-table-column>
			<el-table-column prop="employee.username" label="超级管理员">
			</el-table-column>
			<el-table-column prop="address" label="公司地址">
			</el-table-column>
			<el-table-column prop="logo" label="LOGO">

			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small"  @click="edit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

	</section>
</template>

<script>
	export default {
		data() {
			return {
                listLoading:false,
				//查询对象
				filters:{
					keywords:''
				},
				page:1,//当前页,要传递到后台的
				total:0, //分页总数
			    tenants:[], //当前页数据
                title:"",	//新增编辑弹出框标题
                dialogFormVisible:false,	//新增编辑弹出框默认关闭
                form: {//新增编辑弹出框数据
					id:"",//id
                    companyName:"",//机构名称
                    companyNum:"",//营业执照
                    typeId:"",	//机构类型
                    employeeId:"",//超级管理员
                    address:"",//公司地址
                    logo:"",//lgo
				},
                TenantTypes:[],//机构类型
                employees:[],//超级管理员

			}
		},
		methods: {
            handleCurrentChange(curentPage){
                this.page = curentPage;
                this.getTenants();
			},
			//保存数据
            saveTenant(){
                //console.debug(this.form)
				//创建tenantType对象
				let tenantType = {"id":this.form.typeId};
				this.form.type = tenantType;
				//创建emp对象
				let emp = {"id":this.form.employeeId};
				this.form.employee = emp;
				this.$http.put("/system/tenant",this.form).then(res=>{
					console.debug(res);
					if(res.data.success){
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        //成功后刷新列表
						this.getTenants();
					}else{
                        this.$message.error("操作失败");
					}
                    this.dialogFormVisible = false;
				})
			},
			//编辑机构
            edit(row){
                console.debug(row)
				//弹出模态框
                this.dialogFormVisible = true;
                this.title = "修改";
                if(this.$refs["form"]){     //判断是否校验有值，有值才清空
                    this.$refs["form"].resetFields();
                }
                //回显表单的缓存
                for(var k in this.form){
                    this.form[k] = row[k];
                }
                this.findAllTenantTypes();
                this.findAllEmp();
                //特殊字段回显（机构类型和超级管理员）
				//有机构类型才进行回显
				if(row.type){
                    this.form.typeId = row.type.id;
				}
				//有超级管理员才回显
				if(row.employee){
                    this.form.employeeId = row.employee.id;
				}
			},
			//新增机构
            addTenants(){
                if(this.$refs["form"]){     //判断是否校验有值，有值才清空
                    this.$refs["form"].resetFields();
                }
                //清除表单的缓存
				for(var k in this.form){
					this.form[k] = "";
				}
                this.dialogFormVisible = true;
                this.title = "新增";
                this.findAllTenantTypes();
                this.findAllEmp();
			},
			//查询所有机构类型
			findAllTenantTypes(){
                this.$http.get("/system/tenantType").then(res=>{
                    //console.debug(res);
					//给机构类型进行赋值
					this.TenantTypes = res.data;
					//console.debug(this.TenantTypes);
				})
			},
			//查询所有超级管理员
			findAllEmp(){
                this.$http.get("/system/employee").then(res=>{
                    //console.debug(res);
					this.employees = res.data;
				})
			},
            getTenants(){
                //发送Ajax请求后台获取数据  axios
				//添加分页条件及高级查询条件
				let para = {
				    "page":this.page,
					"keyword":this.filters.keywords
				};
				this.listLoading = true; //显示加载圈
				//分页查询
                this.$http.post("/system/tenant/list",para) //$.Post(.....)
                    .then(result=>{
                        this.total = result.data.total;
                        this.tenants = result.data.rows;
                        this.listLoading = false;  //关闭加载圈
                    });
				/* 查询所有
				this.$http.patch("/department",para) //$.Post(.....)
					.then(result=>{
                        console.log(result);
                        console.log(result.data);
                        this.tenants = result.data;
                        this.listLoading = false;  //关闭加载圈
                    });
                    */
			}
		},
		mounted() {
		    this.getTenants()
		}
	}

</script>

<style scoped>

</style>